package com.msy.rabbit.demo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    @Value("${timeout}")
    private long timeout;

    @Bean
    public Queue queue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", timeout);
        arguments.put("x-dead-letter-exchange", "ex.dlx");//死信队列
        arguments.put("x-dead-letter-routing-key", "key.dlx");//死信队列 routing-key
        return new Queue("queue.biz", true, false, false, arguments);
    }

    @Bean
    public Queue dlxQueue(){
        return new Queue("queue.dlx",true, false, false, null);
    }

    @Bean
    public Exchange exchange() {
        return new DirectExchange("ex.biz", true, false, null);
    }

    @Bean
    public Exchange dlxChange(){
        return new DirectExchange("ex.dlx", true, false, null);
    }

    @Bean
    public Binding binding() {
        return new Binding("queue.biz",
                Binding.DestinationType.QUEUE,
                "ex.biz",
                "key.biz",
                null);
    }

    @Bean
    public Binding dlxBinding(){
        return new Binding("queue.dlx", Binding.DestinationType.QUEUE, "ex.dlx", "key.dlx", null);
    }

}
